<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/20
 * Time: 16:26
 */
namespace app\index\business;
use app\index\business\BaseBusiness;
use app\index\model\CompanyComment;
use app\libs\Predis;
use app\libs\SendSms;
use think\Config;
use think\Db;

class Company extends BaseBusiness
{
    /**
     * 企业添加评论
     * @param $reqData
     * @param $uuid
     * @param $tokenPassword
     */
    public function companyCommentAdd($reqData,$uuid,$tokenPassword){
        $field = ['username'];
        $user = $this->getBusiness('RedisCache')->getUserInfo($uuid,$tokenPassword,$field);
        if (!$user['username']){
            return $this->error('用户不存在');
        }

        $comment['company_id'] = $reqData['identification'];
        $comment['user_no'] = $uuid;
        $comment['content'] = $reqData['content'];
        $comment['username'] = $user['username'];
        $comment['star_num'] = $reqData['star_num'];
        $comment['status'] = 1;
        $comment['created_at'] = $comment['updated_at'] = time();

        // 启动事务 update
        Db::startTrans();
        try {
            //update一条用户信息
            CompanyComment::create($comment);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->result['status'] = -1;
            $this->result['msg'] = '评论失败';
            return $this->result;
        }

        $this->result['status'] = 1;
        $this->result['msg'] = 'success';
        return $this->result;
    }

    /**
     * 企业评论列表
     * @param $company_id
     * @param $page
     * @param $limit
     * @return array
     */
    public function companyCommentList($company_id,$page,$limit)
    {
        $page = bcadd($page,1);
        $data = Db::name('comment_type_company')
            ->where('status','=',1)
            ->where('company_id',$company_id)
            ->order('created_at','desc')
            ->page($page)
            ->limit($limit)
            ->select();

        $count = Db::name('comment_type_company')
            ->where('status','=',1)
            ->where('company_id',$company_id)
            ->count('id');

        $result = compact('count','data');
        if (!$result){
            return $this->success('资源为空',1,$result);
        }
        return $this->success('successs',1,$result);
    }
}